package cn.wangchao.basic.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;


@Component
public class EmailUtil implements ApplicationContextAware {

    private static JavaMailSender javaMailSender;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        javaMailSender = applicationContext.getBean(JavaMailSender.class);
    }


    /**
     * 发送简单邮件
     */
    public static void sendSimpleMail(String from,String subject,String text,String to){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //设置发送人
        simpleMailMessage.setFrom(from);
        //邮件主题
        simpleMailMessage.setSubject(subject);
        //邮件内容
        simpleMailMessage.setText(text);
        //收件人
        simpleMailMessage.setTo(to);

        javaMailSender.send(simpleMailMessage);
    }

}